<template>
  <div class="body-item-li">
    <div style="width: 100%;height: 100%">
      <el-skeleton
        v-if="temp.id === undefined || temp.id === null"
        style="padding: 20px;box-sizing: border-box"
        :rows="height"
      />
      <template-new
        :temp="temp"
        :layout="style"
        @fetch-data="tempCallBack"
        @fetch-entity="entityTempCallBack"
      />
    </div>
  </div>
</template>
<script>
import { getToken } from '@/utils/auth';
import { deleteObject } from '@/api/portal/manager/column';
import { queryEntity, queryGuestEntity } from '@/api/portal/manager/columnSet';
import TemplateNew from '@/template/index';
export default {
  name: 'PortalNewBodyItem', // 栏目组件
  components: { TemplateNew },
  props: {
    layout: {
      type: Object,
      default: () => {
        return {};
      }
    },
    height: {
      type: Number,
      default: 4
    },
    menu: {
      type: Number,
      default: null
    },
    column: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      defaultColumn: {
        x: 0,
        y: 0,
        w: 6,
        h: 8,
        menu: {
          id: this.menu
        }
      },
      rowId: [],
      temp: {
        column: {
          id: this.column
        }
      },
      style: {}
    };
  },
  watch: {
    layout: {
      handler(newName, oldName) {
        // 值发生变化时
        this.style = newName;
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    menu: {
      handler(newName, oldName) {
        // 值发生变化时
        this.defaultColumn.menu = { id: newName };
      },
      immediate: true, // 父组件第一次传值时，执行handler，false则第一次不执行
      deep: false // formType为对象时，设置为true，此属性监听对象发生变化
    },
    column: {
      handler(newName, old) {
        this.initColumnSet(newName);
      },
      immediate: true
    }
  },
  methods: {
    initColumnSet(id) {
      if (getToken()) {
        queryEntity({ id: id })
          .then(res => {
            const { data } = res;
            if (undefined !== data && data !== null) {
              this.temp = data;
            }
          })
          .catch(() => {
            this.temp = { column: { id: this.column }};
          });
      } else {
        queryGuestEntity({ id: id })
          .then(res => {
            const { data } = res;
            if (undefined !== data && data !== null) {
              this.temp = data;
            }
          })
          .catch(() => {
            this.temp = { column: { id: this.column }};
          });
      }
    },
    addColumn() {
      this.$refs.dialog.init({ id: this.menu });
    },
    addBack() {
      this.$emit('callAddBack');
    },
    delColumn() {
      if (undefined !== this.column && this.column !== null) {
        this.$confirm('此操作将永久删除此栏目是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            this.rowId = [];
            this.rowId.push(this.column);
            deleteObject(this.rowId)
              .then(res => {
                this.$message.success('删除成功！');
                this.rowId = [];
                this.$emit('callAddBack');
              })
              .catch(() => {
                this.$message.error('删除失败！');
              });
          })
          .catch(() => {
            this.$message.success('取消删除成功！');
          });
      } else {
        this.$message.warning('删除项不存在！');
      }
    },
    addTemplate() {
      //  this.$refs.set.initObject(this.temp)
    },
    callBack() {
      this.initColumnSet(this.column);
    },
    tempCallBack(data, lay) {
      // 组件点击更多回调
      this.$emit('moreClick', data, lay);
    },
    entityTempCallBack(data) {
      this.$emit('entity-temp', data);
    }
  }
};
</script>
<style scoped>
.body-item-li {
  width: 100%;
  height: 100%;
  padding: 0px;
  box-sizing: border-box;
}
.body-item-li:hover {
  display: flex;
  flex-flow: row nowrap;
}
</style>
